<template>
  <Dialog title="选择语音文件" v-model="dialogVisibleCall" width="800">
    <div class="call-dialog">
      <el-table
        :data="list"
        style="width: 100%"
        :row-key="rowKey"
        ref="tableRef"
        @select="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="name" label="文件名称" align="center"> </el-table-column>
        <el-table-column prop="remark" label="备注"></el-table-column>
        <el-table-column prop="path" label="文件路径" align="center" show-overflow-tooltip>
        </el-table-column>
        <el-table-column
          prop="createTime"
          :formatter="dateFormatter"
          label="创建时间"
          align="center"
        >
        </el-table-column>
      </el-table>
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
      <div class="operation">
        <el-button @click="submitForm" type="primary">确 定</el-button>
        <el-button @click="dialogVisibleCall = false">取 消</el-button>
      </div>
    </div>
  </Dialog>
</template>

<script setup>
import { RecordApi } from '@/api/call/record'
import { dateFormatter } from '@/utils/formatTime'
import * as taskApi from '@/api/call/task'

const dialogVisibleCall = ref(false)
const list = ref([])
const total = ref(0)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10
})
const rowKey = (row) => {
  return row.id
}
const selectValue = ref(null)
const tableRef = ref(null)
const handleSelectionChange = (selection) => {
  let selectRow = {}
  if (selection.length > 1) {
    let del_row = selection.shift()
    tableRef.value.toggleRowSelection(del_row, false)
  }
  // 到这selection数据始终为1条
  if (selection.length) {
    selectRow = selection[0]
  }
  selectValue.value = selectRow
}
const open = () => {
  dialogVisibleCall.value = true
  getList()
}
const getList = async () => {
  const data = await taskApi.getVoiceFilePage(queryParams)
  list.value = data.list
  total.value = data.total
}
const emit = defineEmits(['changeVoice'])
const submitForm = () => {
  emit('changeVoice', selectValue.value)
  dialogVisibleCall.value = false
}
defineExpose({ open })
</script>

<style lang="scss" scoped>
.operation {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 20px 0;
}
::v-deep .el-table__header-wrapper .el-checkbox {
  visibility: hidden;
}
</style>
